package com.cooksys.film_rental.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.FilmActorBo;
import com.cooksys.film_rental.dao.FilmActorDao;
import com.cooksys.film_rental.model.Actor;
import com.cooksys.film_rental.model.Film;
import com.cooksys.film_rental.model.FilmActor;
import com.cooksys.film_rental.model.FilmActorId;

@Service("FilmActorBo")
public class FilmActorBoImpl implements FilmActorBo {

	@Autowired(required = true)
	FilmActorDao filmActorDao;

	@Override
	public void save(FilmActor filmActor) {
		this.filmActorDao.save(filmActor);
	}

	@Override
	public void update(FilmActor filmActor) {
		this.filmActorDao.update(filmActor);
	}

	@Override
	public void delete(FilmActor filmActor) {
		this.filmActorDao.delete(filmActor);
	}

	@Override
	public FilmActor getFilmActorByFilmActorId(FilmActorId filmActorId) {
		return this.filmActorDao.getFilmActorByFilmActorId(filmActorId);
	}

	@Override
	public FilmActor getFilmActorByFilmActorId(Short filmId, Short actorId) {
		FilmActorId filmActorId = new FilmActorId();
		filmActorId.setActorId(actorId);
		filmActorId.setFilmId(filmId);
		return this.getFilmActorByFilmActorId(filmActorId);
	}

	@Override
	public FilmActor getFilmActorByFilmActorId(String filmId, Short actorId) {
		try {

			Short filmIdAsShort = Short.valueOf(filmId);
			return this.getFilmActorByFilmActorId(filmIdAsShort, actorId);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for filmId from string: "
							+ filmId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public FilmActor getFilmActorByFilmActorId(Short filmId, String actorId) {
		try {

			Short actorIdAsShort = Short.valueOf(actorId);
			return this.getFilmActorByFilmActorId(filmId, actorIdAsShort);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for actorId from string: "
							+ filmId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public FilmActor getFilmActorByFilmActorId(String filmId, String actorId) {
		try {

			Short filmIdAsShort = Short.valueOf(filmId);
			Short actorIdAsShort = Short.valueOf(actorId);
			return this
					.getFilmActorByFilmActorId(filmIdAsShort, actorIdAsShort);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for filmId from string: "
							+ filmId
							+ " or Short value for actorId from string: "
							+ actorId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public List<FilmActor> searchFilmActorsByFilm(Film film) {
		return this.filmActorDao.searchFilmActorsByFilm(film);
	}

	@Override
	public List<FilmActor> searchFilmActorsByActor(Actor actor) {
		return this.filmActorDao.searchFilmActorsByActor(actor);
	}

}
